// pages/movies/more-movies.js
let app = getApp()
let util = require("../../util/util.js")

Page({
  /**
   * 页面的初始数据
   */
  data: {
    base_url: app.globalData.tmdb_config.base_url,
    poster_sizes: app.globalData.tmdb_config.poster_sizes,
    movie_listid: 0,
    movie_title: "",
    movies: [],
    movie_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //取得链接传递来的参数
    console.log("收到了 " + options.listid);
    this.setData({
      movie_listid: parseInt(options.listid),
      movie_title: app.globalData.tmdb_query_url[options.listid].title
    });
    util.Movies_RequestMovieList(util.Movies_GetListUrlFromListId(options.listid, 0), parseInt(options.listid), 0, this.Movies_SetPage)
  },

  /**
   * 回调函数，获取movies数组后，加到data中
   */
  Movies_SetPage: function(movs, idx, movie_lens) {
    console.log("--Movies_SetPage")
    if (movs.length > 0) {
      //添加 星数数组
      for (var index in movs) {
        movs[index].stars = util.voteToStarsArray(movs[index].vote_average)
      };
      let arr = [...this.data.movies, ...movs] //合并2个数组
      this.setData({
        movies: arr,
        movie_page: this.data.movie_page + 1
      });
      //隐藏 loading 提示框
      // wx.hideLoading();
      wx.hideNavigationBarLoading();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    //设定导航栏标题
    wx.setNavigationBarTitle({
      title: this.data.movie_title,
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    console.log(this.data.movie_page);
    //显示 loading 提示框
    // wx.showLoading({
    //   title: '加载中...',
    // });
    wx.showNavigationBarLoading();
    util.Movies_RequestMovieList(util.Movies_GetListUrlFromListId(this.data.movie_listid, this.data.movie_page), this.data.movie_listid, 0, this.Movies_SetPage);
  },

  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    console.log("onPullDownRefresh")
  },
})